local bullet = class("bullet",Base)
bullet.speed = 400
bullet.aabb = true
bullet.tag = "bullet"
bullet.ax = 2
bullet.ay = 3
bullet.life = 5
bullet.range = 500
local collsize = 6
function bullet:init(game,x,y,rot,scale,team,iron,range,speed,sender)
    self.game = game
    self.ox = x
    self.oy = y
    self.x = x
    self.y = y
    self.rot = rot
    self.scale = scale
    self.team = team
    self.iron = iron
    self.range = range
    self.speed = speed
    self.sender = sender
    self.game.world:add(self,self.x-collsize/2*self.scale,
        self.y-collsize/2*self.scale,collsize*self.scale,collsize*self.scale)
    if not SERVER then
        if self.game.player and self.game.player.team == team then
            sound:play("fire")
        end
    end
    self:createData(x,y,rot,scale,team,iron,range,speed,sender)
end


function bullet.collFilter(me,other)
    return bump.Response_Cross
end

function bullet:collision(cols) 
    for i,col in ipairs(cols) do
        local other = col.other
        if self.destroyed then return end
        if not other.destroyed then
            if other.tag == "brick" then
               self:destroy()
            elseif other.tag == "iron" then
                self:destroy()
                if self.game.player and self.game.player.id == self.sender then
                    sound:play("touchborder")
                end
            elseif other.tag == "tank" and other.team~=self.team then
                self:destroy()
                --sound:play("touchiron")
            elseif other.tag == "border" then
                self:destroy()
                if self.game.player and self.game.player.id == self.sender then
                    sound:play("touchborder")
                end
            elseif other.tag == "bullet" and other.team~=self.team then
                self:destroy()
                other:destroy()
                sound:play("touchiron")
            elseif other.tag == "hq" then
                self:destroy()
                sound:play("touchiron")
            end
        end
    end
end

function bullet:collision_local(cols)
    for i,col in ipairs(cols) do
        local other = col.other
        if self.destroyed then return end
        if not other.destroyed then
            if other.tag == "brick" then
               self.game.map:clearTile(other.x,other.y)
               self:destroy()
            elseif other.tag == "iron" then
                if self.iron then
                    self.game.map:clearTile(other.x,other.y)
                end
                if not SERVER then
                    if self.game.player and self.game.player.id == self.sender then
                        sound:play("touchborder")
                    end
                end
                self:destroy()
            elseif other.tag == "tank" and other.team~=self.team then
                other:damage(self.game.objects[self.sender])
                if not SERVER then
                    sound:play("touchiron")
                end
                self:destroy()
            elseif other.tag == "border" then
                if not SERVER then
                    if self.game.player and self.game.player.id == self.sender then
                        sound:play("touchborder")
                    end
                end
                self:destroy()
            elseif other.tag == "bullet" then
                self:destroy()
                other:destroy()
                if not SERVER then
                    sound:play("touchiron")
                end
            elseif other.tag == "hq" then
                other:damage(self.game.objects[self.sender])
                if not SERVER then
                    sound:play("touchiron")
                end
                self:destroy()
            end
        end
    end
end



function bullet:update(dt)
    if self.destroyed then return end
    self.life = self.life - dt
    if self.life<0 then self:destroy() ;return end

    if math.abs(self.x-self.ox) + math.abs(self.y - self.oy) > self.range then
        self:destroy()
        return
    end

    if self.rot == 0 then
        self.y = self.y - self.speed*dt
    elseif self.rot == 1 then
        self.x = self.x + self.speed*dt
    elseif self.rot == 2 then
        self.y = self.y + self.speed*dt
    elseif self.rot == 3 then
        self.x = self.x - self.speed*dt
    end
    self.x, self.y ,cols = self.game.world:move(self,self.x-collsize/2*self.scale,self.y-collsize/2*self.scale,self.collFilter)
    self.x, self.y = self.x +collsize/2*self.scale , self.y + collsize/2*self.scale
    if net.inGame then
        self:collision(cols)
    else
        self:collision_local(cols)
    end
end

function bullet:draw()
    love.graphics.setColor(1,1,1)
    texture:draw("bullet",self.x,self.y,self.rot*math.pi/2,self.scale,self.scale,self.ax,self.ay)
end

function bullet:destroy()
    if self.destroyed then return end
    Base.destroy(self)
    if not SERVER then
        Ding(self.game,math.floor(self.x),math.floor(self.y),self.scale,0.3)
    end
end

return bullet